﻿ 


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.IO;

namespace IGIS
{


    /// <summary>
    /// Settings for the project
    /// </summary>
    public class MapSettings
    {
        #region Private fields
        private string _mapguideUrl;
        private string _mapGuideUser;
        private string _mapguidePass;

        private int _projectID;
        private string _libraryName;
        private string _projectPath;
        private string _mapName;
        private string _layoutName;
        private bool _searchAllowed;
        private string _searchLayer;
        private string _searchFeature;
        private string _searchKeyName;
        private string _searchTextField;
        #endregion

        #region Properties Section
        public int ProjectId { get { return _projectID; } }
        public string LibraryName { get { return _libraryName; } }
        public string ProjectPath { get { return _projectPath; } }
        public string MapName { get { return _mapName; } }
        public string LayoutName { get { return _layoutName; } }
        public bool SearchAllowed { get { return _searchAllowed; } }
        public string SearchLayer { get { return _searchLayer; } }
        public string SearchFeature { get { return _searchFeature; } }
        public string SearchKeyName { get { return _searchKeyName; } }
        public string SearchTextField { get { return _searchTextField; } }
        public string MapDefinition
        {
            get { return string.Format("{0}/Maps/{1}.MapDefinition", _projectPath, MapName); }
        }
        public string LayoutDefinition
        {
            get { return string.Format("{0}/Layouts/{1}.WebLayout", _projectPath, LayoutName); }
        }
        public string FeaturePath
        {
            get { return string.Format("{0}/Data/{1}.FeatureSource", _projectPath, SearchFeature); }
        }
        public string SearchLayerDefinition
        {
            get { return string.Format("{0}/Layers/{1}.LayerDefinition", _projectPath, SearchLayer); }
        }
        #endregion

        #region The constructor
        public MapSettings(int ProjectId)
        {
            string path = Path.Combine(HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"], @"XML\mgProjects.xml");

            if (!File.Exists(path))
            {
                throw new InternalSiteException("Settings file not found");
            }

            XmlDocument mgProjects = new XmlDocument();
            mgProjects.Load(path);

            XmlNode Settings = mgProjects.SelectNodes(string.Format("/PROJECTS/PROJECT[@ID=\"{0}\"]", ProjectId))[0];
            if (Settings == null)
            {
                throw new InternalSiteException("Project not found");
            }

            try
            {
                _projectID = ProjectId;
                _mapName = Settings.SelectSingleNode("MAP").InnerText;
                _layoutName = Settings.SelectSingleNode("LAYOUT").InnerText;
                _projectPath = Settings.Attributes["PATH"].Value;

                //Allow serch?
                string sa = Settings.SelectSingleNode("SEARCH").Attributes["ALLOWED"].Value;
                if (sa == "true") { _searchAllowed = true; }
                else { _searchAllowed = false; }

                if (_searchAllowed)
                {
                    _searchFeature = Settings.SelectSingleNode("SEARCH/FEATURE").InnerText;
                    _searchKeyName = Settings.SelectSingleNode("SEARCH/KEYNAME").InnerText;
                    _searchTextField = Settings.SelectSingleNode("SEARCH/TEXTFIELD").InnerText;
                    _searchLayer = Settings.SelectSingleNode("SEARCH/LAYER").InnerText;
                }
            }
            catch (Exception ex)
            {
                throw new InternalSiteException("Invalid configuration file");
            }
        }
        #endregion
    }

    public class InternalSiteException : Exception
    {
        public InternalSiteException(string Message)
            : base(Message)
        { }
        public InternalSiteException(string message, Exception innerException)
            : base(message, innerException)
        { }
    }
}